Skip to content

Walk a hub row alone and reuse a walk for later pages in the Postgres store - #712

Merged
arrry-hydraDB merged 1 commit into
adityamishra/oss-136-pg-store-tsfrom
adityamishra/oss-136-pg-walk-cache
Dec 4, 2026
Merged

arrry-hydraDB merged 1 commit into
adityamishra/oss-136-pg-store-tsfrom
adityamishra/oss-136-pg-walk-cache

Conversation

@adityamishra-hydradb

@adityamishra-hydradb adityamishra-hydradb commented Dec 4, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

4/5 of #706. After pg_hydradb cuts a row's edges once, the Postgres store walks that row on its own, so it no longer cuts the rows walked with it or forces a split on every read.
The store also reuses its last walk when retrieval pages through the same edges. Both SDKs.

Implementation details

Door: two-way
Store internals only; no API, id, or output change.

Cleanup: none

What to delete:

Decisions

  • A row with more than 5,000 edges makes every walk that holds it come back cut, so 2/5 splits the batch in halves until the row is alone, and pg_hydradb loads all of the row's edges at each split (about 0.5 s at 50,000 edges). The store now remembers such rows (per direction, up to 4,096) and walks them alone up front. A remembered row that comes back complete is forgotten.
  • Retrieval reads adjacency in pages of 500 (skip, or id_gt for keyset reads). The store keeps the last 8 walks and serves a later page (skip > 0 or id_gt set) from them; a first page always walks again, so each read still sees current rows. A cached cut read marks the result partial again, and a closed store raises store_closed before the cache is read.
  • CI (decided by the maintainers on 2026-12-05): #729 adds the CI job postgres. It builds pg_hydradb from a pinned hydradb commit and runs these gated tests of both SDKs on Postgres 16 and 17. No image goes to a registry.
  • These are a separate PR because they are an optimisation with their own numbers, and the stores in 2/5 and 3/5 are near the 1,000-line limit.

Validation

  • New gated test in both SDKs: two retrieves from a 5,101-edge company and a small one. The second sends fewer walks and walks the big company alone; both results are truncated. A neighbors read of the big company then walks it once per direction across all its pages, and a later page after close raises store_closed. It fails without this change.
  • Stress on 1M people, 10k companies, 3M edges (one company with 50,094 employees): one retrieve that reaches the big company went from 20.9 s (159 walks) to 6.7 s (40 walks). Medians for ordinary questions: retrieve 289 ms, neighbors 4.6 ms, find_path 42 ms (with a trigram index on the names).
  • precheck --base 3/5: all checks passed, with a conformance warning: no result changes here, so there is no new conformance case.
  • Full suites and probes: see 5/5.

Limits

  • The memory of cut rows is per store instance and is cleared at 4,096 entries.

Linked issue

Closes #706

AI Assistance

Model Effort
Claude Opus 5.5 High

Checklist

  • Implements an issue a maintainer has /approved.
  • Small and scoped to that issue.
  • New errors have a code and hint, the same on API, CLI, and MCP in both SDKs (see AGENTS.md).
  • Breaking changes described below, or none.

Breaking changes

None.

🤖 Generated with Claude Code

@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

@hydra-mira-review

hydra-mira-review Bot commented Dec 4, 2026 •

Copy link
Copy Markdown

Mira score 5/5 · 🟢 Good to merge

  • No findings in the code.
  • Read these high-risk changes closely: Reuse adjacency reads for later pages.

Reads involving highly connected rows avoid repeated batch splitting and reuse work across later pages in both SDKs. The Postgres stores remember rows needing isolated walks and cache recent walks, while first pages still refresh.

How this PR works · 3 changes, 1 high risk, 1 medium risk, 1 low risk

Reads involving a heavily connected row repeat expensive walks, both while splitting batches and while paging through edges. This PR remembers rows whose walks were cut short and walks them alone on later reads. Both SDKs also cache eight adjacency reads for later pages, while refreshing every first page.

1. Walk previously cut rows alone on later reads · 🟠 medium risk · typescript/src/stores/pgHydradb.ts +1

Repeated reads avoid splitting batches around a known heavily connected row. Previously every read rediscovered that row; now walk and _walk remember single-row cuts separately for each direction.

Risk: Incorrect grouping makes Postgres graph reads repeat expensive walks or omit relations from retrieval and neighbor results.

Check:

  • Read a 5,101-edge company alongside a small company twice; confirm fewer walks on the second read and unchanged truncation.
  • Reduce the remembered company's edges below the limit; confirm a complete walk removes its remembered entry for that direction.

In pgHydradb.ts:143-145, pgHydradb.ts:632-660, pgHydradb.ts:681-691 and 2 more.

2. Reuse adjacency reads for later pages · 🔴 high risk · typescript/src/stores/pgHydradb.ts +1

Later pages can reuse already loaded edges instead of repeating walks and row lookups. First pages still refresh; adjacent and _adjacent retain eight reads.

Risk: Incorrect cache selection or partial-state replay silently gives graph readers stale or wrong relations, or hides that a result was cut short.

Check:

  • Compare offset and keyset pages with a fresh read, including changed relation filters and more than eight cached endpoint sets.
  • Change an edge between pages; confirm later-page reuse, first-page refresh, retained truncation and store_closed after closing.

How it works:

  1. matchEdges and match_edges allow reuse when skip > 0 or id_gt is set.
  2. adjacent and _adjacent validate the namespace and store lifecycle before consulting the cache.
  3. The cache key contains the namespace, direction and sorted starting IDs.
  4. A cache hit restores the partial-read marker when the saved walk was cut.
  5. A fresh read saves hydrated edges and the partial flag, replacing its previous entry and evicting the oldest insertion beyond eight entries.
  6. matchEdges and match_edges apply filters, ordering and pagination after obtaining the cached or refreshed edges.

What it touches:

  • typescript/src/ops.ts: listRelations, allEdges.
  • typescript/src/retrieval.ts: adjacent.
  • src/selfgraph/_ops.py: list_relations, _edges_into.
  • src/selfgraph/retrieval.py: _adjacent.
  • SDK retrieval and neighbor reads that page through endpoint-filtered relations.
  • Relation listing with offset pagination or a keyset cursor.
  • Caches hydrated edge rows in memory. Later pages can reuse earlier row values; a first page replaces them with fresh values. Persistent schemas and response shapes stay unchanged.

In pgHydradb.ts:43-44, pgHydradb.ts:219-220, pgHydradb.ts:673-680 and 7 more.

  1. Supporting changes · 🟢 low risk · tests in pgHydradbStore.test.ts:191-216, test_pg_hydradb_store.py:183-207.
Requirement check · 7 met · from the PR description
  • ✅ Remember cut rows per direction and walk them alone on subsequent reads in both SDKs (met). walk and _walk record single-row cuts; adjacent and _adjacent separate remembered rows from ordinary batches.
  • ✅ Forget a remembered row after a complete walk and bound remembered rows at 4,096 per store (met). walk and _walk remove single-row entries before recording any new cut, and clear the set at its capacity.
  • ✅ Retain eight adjacency reads and reuse them for offset or keyset continuation pages (met). matchEdges and match_edges select continuation reads; both adjacency methods store eight entries keyed by namespace, direction and sorted IDs.
  • ✅ Always refresh a first page (met). adjacent and _adjacent return cached rows only when the continuation argument is true.
  • ✅ Preserve partial results on cache hits and reject reads after closing before consulting the cache (met). Both adjacency methods open the store before cache access and restore the saved partial flag through markPartial or mark_partial.
  • ✅ Keep public APIs, IDs and output formats unchanged (met). matchEdges and match_edges retain their public signatures, relation encoding, filtering and pagination; the new arguments belong to private helpers.
  • ✅ Add matching gated integration cases for walk reduction, truncation, pagination reuse and closed-store rejection (met). typescript/test/pgHydradbStore.test.ts and tests/test_pg_hydradb_store.py add matching cases with a 5,101-edge company.
Diagram of the changed files
graph LR
  tstest["typescript/test/pgHydradbStore.test.ts"] --> tsstore["typescript/src/stores/pgHydradb.ts"]
  pytest["tests/test_pg_hydradb_store.py"] --> pystore["src/selfgraph/stores/pg_hydradb.py"]
Loading
⚠️ Other open PRs change the same code · 1

Merge them in order, or check that they agree.

Comment @hydra-mira-review help for commands.

@hydra-mira-review hydra-mira-review Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mira Review Summary

Both retrieval implementations use offset pagination, so the new cache does not serve continuation pages; Python also reloads all adjacent edges for each page (pg_hydradb.py). Cached relation pages can still be read after either store closes, and both new regression tests are skipped in CI (pgHydradbStore.test.ts).

Key Issues

Issue Location
🔴 Both retrieval implementations use offset pagination, so the new cache never serves their continuation pages. typescript/src/stores/pgHydradb.ts:746
🔴 Both new regression tests are skipped by every CI test job. typescript/test/pgHydradbStore.test.ts:185

Comment thread typescript/src/stores/pgHydradb.ts Resolved
Comment thread src/selfgraph/stores/pg_hydradb.py Resolved
Comment thread typescript/test/pgHydradbStore.test.ts Resolved
Comment thread src/selfgraph/stores/pg_hydradb.py Resolved
Comment thread typescript/src/stores/pgHydradb.ts Resolved
@adityamishra-hydradb
adityamishra-hydradb force-pushed the adityamishra/oss-136-pg-walk-cache branch from 3bcf814 to 456e4c0 Compare December 4, 2026 21:19
@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

@adityamishra-hydradb
adityamishra-hydradb force-pushed the adityamishra/oss-136-pg-walk-cache branch from 456e4c0 to cb3b0dc Compare December 4, 2026 21:37
@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

@adityamishra-hydradb
adityamishra-hydradb force-pushed the adityamishra/oss-136-pg-walk-cache branch from cb3b0dc to 39fff15 Compare December 4, 2026 21:49
@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

@adityamishra-hydradb
adityamishra-hydradb force-pushed the adityamishra/oss-136-pg-walk-cache branch from 39fff15 to be64d0f Compare December 4, 2026 22:00
@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

… store

Signed-off-by: adityamishra-hydradb <[email protected]>
Co-Authored-By: Claude Opus 5.5 <[email protected]>
@adityamishra-hydradb
adityamishra-hydradb force-pushed the adityamishra/oss-136-pg-walk-cache branch from be64d0f to 3556eeb Compare December 4, 2026 22:11
@adityamishra-hydradb

Copy link
Copy Markdown
Collaborator Author

@hydra-mira-review review

@arrry-hydraDB arrry-hydraDB left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@arrry-hydraDB
arrry-hydraDB merged commit 367cc96 into main Dec 4, 2026
51 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Read Postgres tables through pg_hydradb with a read-only store

2 participants